home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / pcpilot.arc / SCR.ARC / BUF.C next >
Text File  |  1990-01-14  |  3KB  |  137 lines

  1. /*
  2.     BUF.C
  3.     From the book "Systems Programming in Turbo C",  by Michael J. Young
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <process.h>
  8. #include <alloc.h>
  9. #include <dos.h>
  10. #define LIBFILE
  11. #include "buf.h"
  12. #include "scr.h"
  13. #include "kbd.h"
  14.  
  15. #define MAXLINES 4000
  16. static char *LinePtAr [MAXLINES];
  17. static int ULC, ULR, LRC, LRR;
  18. static int LinesPerWin;
  19. static int CharPerLine;
  20.  
  21.  
  22. void BufInit (int StartCol, int StartRow, int StopCol, int StopRow)
  23. /*
  24.     This function initializes the buffer display system of functions
  25. */
  26. {
  27.     ULR = StartRow;                    /* Save dementions of window            */
  28.     ULC = StartCol;
  29.     LRR = StopRow;
  30.     LRC = StopCol;
  31.     LinesPerWin = LRR - ULR + 1;
  32.     CharPerLine = LRC - ULC + 2;
  33.     LastLine = -1;                    /* Initialize global variable           */
  34. }
  35.  
  36.  
  37. char *BufNextLine ()
  38. /*
  39.     This function adds another line to the internal buffer, and returns
  40.     the starting address of the line.
  41. */
  42. {
  43.     int i;
  44.  
  45.     if (++LastLine >= MAXLINES)    {            /* Test for buffer size limit */
  46.         ScrPutS("MAXLINES exceeded!", 0x46, 12, 33);
  47.         exit(1);
  48.     }
  49.  
  50.     if (LastLine % LinesPerWin == 0)    {    /* Time to allocate          */
  51.         if ((LinePtAr [LastLine] =
  52.             (char *)malloc (LinesPerWin * CharPerLine+10)) == NULL) {
  53.             ScrPutS("Out of heap memory", 0x47, 12, 33);
  54.             exit(1);
  55.         }
  56.     }
  57.     else
  58.         LinePtAr [LastLine] = LinePtAr [LastLine - 1] + CharPerLine;
  59.  
  60.     return (LinePtAr [LastLine]);
  61. }
  62.  
  63.  
  64. void BufShow (int StartLine, int Attr)
  65. /*
  66.     This function displays the buffer, beginning with the line having the
  67.     index specified by 'StartLine'.
  68. */
  69. {
  70.     register int Line, Row;
  71.  
  72.     ScrClear (ULC, ULR, LRC, LRR);        /* First clear the window         */
  73.     Row = ULR;
  74.  
  75.     for (Line=StartLine; Line<StartLine+LinesPerWin&&Line<=LastLine; ++Line)
  76.         ScrPutS (LinePtAr [Line], Attr, Row++, ULC);
  77. }
  78.  
  79.  
  80. char *BufGet (int Line)
  81. /*
  82.     This function returns the address of buffer line number 'Line'.
  83. */
  84. {
  85.     if (Line <= LastLine)
  86.         return (LinePtAr [Line]);
  87.     else
  88.         return (NULL);
  89. }
  90.  
  91.  
  92. void BufFree ()
  93. /*
  94.     This function frees the buffer memory.
  95. */
  96. {
  97.     int i;
  98.  
  99.     for (i=0; i<=LastLine; i += LinesPerWin)
  100.         free (LinePtAr [i]);
  101. }
  102.  
  103.  
  104. void BufScroll(int lines, int direc, int attr)
  105. /*
  106.     This function scrolls an area of the screen.
  107. */
  108. {
  109.     union REGS r;
  110.  
  111.     if (direc == 0) r.h.ah = 6;                /* scroll up */
  112.     else r.h.ah = 7;                        /* scroll dn */
  113.  
  114.     r.h.al = lines;                            /* no. of lines to scroll */
  115.     r.h.ch = ULR;
  116.     r.h.cl = ULC;
  117.     r.h.dh = LRR;
  118.     r.h.dl = LRC;
  119.     r.h.bh = attr;                            /* color */
  120.     int86(0x10, &r, &r);
  121. }
  122.  
  123.  
  124. void downward (int line, int numlines, int Attr)
  125. {
  126.     line += LinesPerWin;
  127.     BufScroll(numlines, 0, Attr);
  128.     ScrPutS (LinePtAr [line], Attr, LRR, ULC);
  129. }
  130.  
  131.  
  132. void upward (int line, int numlines, int Attr)
  133. {
  134.     line--;
  135.     BufScroll(numlines, 1, Attr);
  136.     ScrPutS (LinePtAr [line], Attr, ULR, ULC);
  137. }